//
//  MACountContentViewController.m
//  meiang_edu
//
//  Created by 栋 on 2019/1/8.
//  Copyright © 2019年 com.meiang.meiang_edu. All rights reserved.
//

#import "MACountContentViewController.h"
#import "MACountListViewController.h"
#import "MACompleteStatusModel.h"
@interface MACountContentViewController ()<UIScrollViewDelegate>

/**
 UIScrollView,滑动切换视图
 */
@property (weak, nonatomic) IBOutlet UIScrollView *mainScrollView;

/**
 滑动式图容器View
 */
@property (weak, nonatomic) IBOutlet UIView *contentView;

/**
 完成label
 */
@property (weak, nonatomic) IBOutlet UILabel *lblComplete;

/**
 未完成label
 */
@property (weak, nonatomic) IBOutlet UILabel *lblUnComplete;

/**
 标志线  完成label底部的线条,用来表示选中
 */
@property (weak, nonatomic) IBOutlet UIView *viewLine;

/**
 标志线中心约束
 */
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *layoutViewLineCenter;

/**
 切换视图顶部约束
 */
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *layoutSegTop;

/**
 已完成VC
 */
@property (strong, nonatomic) MACountListViewController *completeVC;

/**
 未完成VC
 */
@property (strong, nonatomic) MACountListViewController *unCompleteVC;

/**
 切换标题种类,例如完成/上传/检查 等
 */
@property (nonatomic ,copy) NSString * titleType;
@end

@implementation MACountContentViewController

#pragma mark - init
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
//    查看通知情况
//    活动报名统计
    if (_countType==MACountContentTypeNormal) {
        if ([_funcItemModel.corner_name isEqualToString:@"通知"]) {
            self.title = @"查看通知情况";
            _titleType = @"读";
        } else if ([_funcItemModel.corner_name isEqualToString:@"活动"]) {
            self.title = @"活动报名统计";
            _titleType = @"报名";
        }else {
            self.title = @"检查完成详情";
            _titleType = @"完成";
        }
    } else if (_countType==MACountContentTypeWorkCheck){
        self.title = @"检查完成详情";
        _titleType = @"完成";
    }else{
        self.title = @"检查上传作业";
        _titleType = @"上传";
    }
    self.layoutSegTop.constant = [UIApplication sharedApplication].statusBarFrame.size.height + 44;
    [self.view layoutIfNeeded];
    self.lblComplete.text = [NSString stringWithFormat:@"已%@",_titleType];
    self.lblUnComplete.text = [NSString stringWithFormat:@"未%@",_titleType];
    [self setUpChildView];
    [self loadDataWithCompleteBlock:nil];
}

//初始化已完成,未完成视图
- (void)setUpChildView{
    _completeVC = [MACountListViewController new];
    _completeVC.finishType = MACountListViewFinishTypeFinished;
    _completeVC.parentVC = self;
    [self.contentView addSubview:_completeVC.view];
    [self addChildViewController:_completeVC];
    
    _unCompleteVC = [MACountListViewController new];
    _unCompleteVC.finishType = MACountListViewFinishTypeUnFinished;
    _unCompleteVC.parentVC = self;
    [self.contentView addSubview:_unCompleteVC.view];
    [self addChildViewController:_unCompleteVC];
    
    [_completeVC.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.mas_equalTo(0);
        make.width.mas_equalTo(SCREEN_WIDTH);
    }];
    [_unCompleteVC.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.bottom.mas_equalTo(0);
        make.width.mas_equalTo(SCREEN_WIDTH);
    }];

}

//加载数据
- (void)loadDataWithCompleteBlock:(void(^)(void))completeBlock{
    
    NSString * type = @"zy";
    NSLog(@"_model.message_type = %@",_model.message_type);
    if (![_model.message_type isEqualToString:@"作业"]) {
        type = _model.message_typechild;
    }
    NSString * url = @"";
    if (_countType == MACountContentTypeNormal) {
        url = [NSString stringWithFormat:@"%@%@?type=%@",NOTICE_MESSAGE_READ,_model.keyid,type];
    }else if (_countType == MACountContentTypeWorkCheck) {
        url = [NSString stringWithFormat:@"%@%@?type=check",NOTICE_MESSAGE_READ,_model.keyid];
    }else{
        url = [NSString stringWithFormat:@"%@%@?type=upload",NOTICE_MESSAGE_READ,_model.keyid];
    }
    [MANetwork GET:url parameters:nil autoShowFailAlert:YES showLoading:NO success:^(id response, BOOL success) {
        if (success) {
            NSDictionary * value = response[@"value"];
            NSArray * unfinishedlst = value[@"unfinishedlst"];
            NSArray *unfinishedModelList = [MACompleteStatusModel mj_objectArrayWithKeyValuesArray:unfinishedlst];
            NSArray * finishedlst = value[@"finishedlst"];
            NSArray *finishedModelList = [MACompleteStatusModel mj_objectArrayWithKeyValuesArray:finishedlst];

            self.unCompleteVC.dataSource = unfinishedModelList;
            self.completeVC.dataSource = finishedModelList;
            self.lblComplete.text = [NSString stringWithFormat:@"已%@(%zd)",_titleType,finishedlst.count];
            self.lblUnComplete.text = [NSString stringWithFormat:@"未%@(%zd)",_titleType,unfinishedlst.count];

        }
        if (completeBlock) {
            completeBlock();
        }
    } failure:^(NSError *error) {
        if (completeBlock) {
            completeBlock();
        }
    }];
}

//修改选中视图
- (void)setSelectedIndex:(NSInteger)selectedIndex{
    if (selectedIndex==0) {
        self.lblComplete.textColor = [UIColor colorWithHexString:@"13A2C6"];
        self.lblUnComplete.textColor = [UIColor colorWithHexString:@"333333"];
    } else {
        self.lblComplete.textColor = [UIColor colorWithHexString:@"333333"];
        self.lblUnComplete.textColor = [UIColor colorWithHexString:@"13A2C6"];
    }
    [UIView animateWithDuration:0.25 animations:^{
        self.layoutViewLineCenter.constant = selectedIndex * self.view.width*0.5;
        [self.view layoutIfNeeded];
    }];
    
}

#pragma mark - 点击事件

- (IBAction)tapComplete:(id)sender {
    
    [self.mainScrollView setContentOffset:CGPointMake(0, 0) animated:YES];
}

- (IBAction)tapUnComplete:(id)sender {
    
    [self.mainScrollView setContentOffset:CGPointMake(SCREEN_WIDTH, 0) animated:YES];
}


#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (scrollView.contentOffset.x>=scrollView.width*0.5) {
        [self setSelectedIndex:1];
    }else if(scrollView.contentOffset.x<=scrollView.width*0.5){
        [self setSelectedIndex:0];
    }
}

@end
